package cn.bluemobi.platform.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bluemobi.common.core.dto.MapDto;
import cn.bluemobi.common.core.dto.Page;
import cn.bluemobi.common.core.dto.PageCondition;
import cn.bluemobi.platform.core.mybatis.PageUtils;
import cn.bluemobi.platform.entity.commodity.Commodity;
import cn.bluemobi.platform.entity.commodity.Specification;
import cn.bluemobi.platform.entity.system.User;
import cn.bluemobi.platform.mapper.CommodityMapper;
import cn.bluemobi.platform.service.CommodityService;

/**
 * 
 * @author lvxh
 * 
 */
@Service
@Transactional
public class CommodityServiceImpl implements CommodityService {

	@Autowired
	private CommodityMapper commodityMapper;

	@Override
	public Page<Commodity> findCommoditys(PageCondition cond, User user) {
		Page<Commodity> page = PageUtils.startPage(cond);
		cond.getMap().put("merchantId", user.getMerchantId());
		page.setList(commodityMapper.findCommoditys(cond.getMap()));
		return page;
	}

	@Override
	public MapDto saveOrUpdate(Commodity commodity, User user) {
		MapDto dto = new MapDto();
		if (StringUtils.isEmpty(commodity.getCommodityId())) {
			// 新增商品
			commodityMapper.insertCommodity(commodity);
			commodity.setCommodityId(commodity.getId());
			// 新增商品图片
			List<String> imgs = commodity.getImageUrls();
			if (imgs != null) {
				commodityMapper.insertCommodityImages(commodity);
			}
			// 新增规格
			String[] specificationImageUrls = (String[]) commodity
					.getSpecificationImageUrls().toArray(new String[0]);
			String[] specificationCurrentPrices = (String[]) commodity
					.getSpecificationCurrentPrices().toArray(new String[0]);
			String[] specificationNames = (String[]) commodity
					.getSpecificationNames().toArray(new String[0]);
			String[] specificationSupplyCounts = (String[]) commodity
					.getSpecificationSupplyCounts().toArray(new String[0]);
			String[] specificationPointss = (String[]) commodity
					.getSpecificationPointss().toArray(new String[0]);
			List<Specification> specifications = new ArrayList<Specification>();
			for (int i = 0; i < specificationImageUrls.length; i++) {
				Specification specification = new Specification();
				specification.setCommodityId(commodity.getCommodityId());
				specification.setImageUrl(specificationImageUrls[i]);
				specification.setSpecificationName(specificationNames[i]);
				specification.setCurrentPrice(specificationCurrentPrices[i]);
				specification.setSupplyCount(specificationSupplyCounts[i]);
				specification.setPoints(specificationPointss[i]);
				specifications.add(specification);
			}
			commodityMapper.insertSpecification(specifications);
		} else {
			// 编辑
			commodityMapper.updateCommodity(commodity);
			// 删除店铺图片
			commodityMapper.deleteCommodityImages(commodity.getCommodityId());
			// 新增店铺图片
			List<String> imgs = commodity.getImageUrls();
			if (imgs != null) {
				commodityMapper.insertCommodityImages(commodity);
			}
			// 删除规格
			// commodityMapper.deleteSpecification(commodity.getCommodityId());
			// 新增规格
			// 新增规格
			String[] specificationIds = (String[]) commodity
					.getSpecificationIds().toArray(new String[0]);
			String[] specificationImageUrls = (String[]) commodity
					.getSpecificationImageUrls().toArray(new String[0]);
			String[] specificationCurrentPrices = (String[]) commodity
					.getSpecificationCurrentPrices().toArray(new String[0]);
			String[] specificationNames = (String[]) commodity
					.getSpecificationNames().toArray(new String[0]);
			String[] specificationSupplyCounts = (String[]) commodity
					.getSpecificationSupplyCounts().toArray(new String[0]);
			String[] specificationPointss = (String[]) commodity
					.getSpecificationPointss().toArray(new String[0]);
			List<Specification> updateSpecifications = new ArrayList<Specification>();
			for (int i = 0; i < specificationIds.length; i++) {
				Specification specification = new Specification();
				specification.setCommodityId(commodity.getCommodityId());
				specification.setSpecificationId(specificationIds[i]);
				specification.setImageUrl(specificationImageUrls[i]);
				specification.setSpecificationName(specificationNames[i]);
				specification.setCurrentPrice(specificationCurrentPrices[i]);
				specification.setSupplyCount(specificationSupplyCounts[i]);
				specification.setPoints(specificationPointss[i]);
				updateSpecifications.add(specification);
			}
			commodityMapper.updateSpecification(updateSpecifications);

			if (specificationIds.length < specificationImageUrls.length) {
				List<Specification> addSpecifications = new ArrayList<Specification>();
				for (int i = specificationIds.length; i < specificationImageUrls.length; i++) {
					Specification specification = new Specification();
					specification.setCommodityId(commodity.getCommodityId());
					specification.setImageUrl(specificationImageUrls[i]);
					specification.setSpecificationName(specificationNames[i]);
					specification
							.setCurrentPrice(specificationCurrentPrices[i]);
					specification.setSupplyCount(specificationSupplyCounts[i]);
					specification.setPoints(specificationPointss[i]);
					addSpecifications.add(specification);
				}
				commodityMapper.insertSpecification(addSpecifications);
			}
		}
		return dto;
	}

	@Override
	public Commodity findCommodityDetail(String commodityId) {
		Commodity commodity = commodityMapper.findCommodityDetail(commodityId);
		if (commodity != null) {
			List<Specification> specifications = commodityMapper
					.findSpecifications(commodityId);
			commodity.setSpecifications(specifications);
		}
		return commodity;
	}

	@Override
	public MapDto deleteCommodity(String commodityId, User user) {
		MapDto dto = new MapDto();
		commodityMapper.deleteCommodity(commodityId);
		return dto;
	}

	@Override
	public List<Map<String, Object>> findCategoryList(String level,
			String parent) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("level", level);
		params.put("parent", parent);
		return commodityMapper.findCategoryList(params);
	}

	@Override
	public List<Map<String, Object>> findMerchantList() {
		return commodityMapper.findMerchantList();
	}
}
